// 寻物与出物
var express = require('express');
var router = express.Router();
var db = require('../model/index');

/* GET  */
router.get('/all', function (req, res) {
    //   res.send('respond with a resource');
    var sql = `select barterId,barterType,barterAudit,barterTitle,barterImg,barterDesc from barterlist where InfoPublisher = ? union ALL select takeId,takeType,takeAudit,takeTitle,takeImg,takeDesc from takelist where InfoPublisher = ? 
UNION ALL select messageId,messageType,messageAudit,messageTitle,messageImg,messageDesc from messagelist where InfoPublisher = ? UNION ALL select shareId,shareType,shareAudit,shareTitle,shareImg,shareDesc from sharelist where InfoPublisher =?`
    // ORDER BY barterId
    db(sql, [req.query.InfoPublisher, req.query.InfoPublisher, req.query.InfoPublisher, req.query.InfoPublisher], function (err, data) {
        if (!err) {
            //    console.log(123);
            if (data.length > 0) {
                res.send({
                    code: 200,
                    message: "success",
                    data: data,
                })

            } else {
                res.send({
                    code: 500,
                    message: "fail"
                })
            }

        } else {
            res.send({
                code: 501,
                message: "dbfail"
            })
        }
    })

});
// 寻物、出物
router.get('/barter', function (req, res) {
    //   res.send('respond with a resource');
    var sql = `SELECT * FROM barterList where barterId = ?`
    db(sql, [req.query.infoId], function (err, data) {
        if (!err) {
            if (data.length > 0) {
                res.send({
                    code: 200,
                    message: "success",
                    data: data,
                })

            } else {
                res.send({
                    code: 500,
                    message: "fail"
                })
            }

        } else {
            res.send({
                code: 501,
                message: "dbfail"
            })
        }
    })

});
// 更新
router.post('/updateBarter', function (req, res) {
    var sql = `UPDATE barterList SET barterContent=?,barterDesc = ?,barterImg=?,barterTags=?,barterTitle=?,barterValidDate=? WHERE barterId = ?`
    db(sql, [req.body.infoContent, req.body.infoDesc, req.body.infoImg, req.body.infoTags, req.body.infoTitle, req.body.infoValidDate, req.body.infoId], function (err, data) {
        //    db(sql,null,function(err,data){

        if (!err) {
            //    console.log(123);
            res.send({
                code: 200,
                message: "success",
                data: data,
            })
        } else {
            res.send({
                code: 501,
                message: "dbfail"
            })
        }
    })

});
// 图片批量更新  delBarterImg
router.post('/delBarterImg', function (req, res) {
    var sql = `DELETE FROM barterimg WHERE barterId = ?`
    db(sql, [req.body.infoId], function (err, data) {
        if (!err) {
            res.send({
                code: 200,
                message: "success",
                data: data,
            })
        } else {
            res.send({
                code: 501,
                message: "dbfail",
            })
        }
    });

});
router.post('/updateBarterImgs', function (req, res) {
    let arr = req.body.infoImgs; // 前端提交过来的数据
    console.log(arr, 11);
    let sql = `INSERT INTO barterimg (barterId,barterImg) VALUES(?,?)`    // 插入语句
    for (let i = 0; i < arr.length; i++) {
        db(sql, [req.body.infoId, arr[i]], function (err, data) {
            if (err) {
                console.log(err)
                if (i == arr.length - 1) {
                    res.send({
                        code: 500,
                        msg: "添加失败!",
                    });
                }
            } else {
                console.log(data);
                if (i == arr.length - 1) {
                    res.send({
                        msg: "添加成功!",
                        code: 200
                    });
                }
            }
        });
    }
});
// 代取
router.get('/take', function (req, res) {
    //   res.send('respond with a resource');
    var sql = `SELECT * FROM takeList where takeId = ?`
    db(sql, [req.query.infoId], function (err, data) {
        if (!err) {
            if (data.length > 0) {
                res.send({
                    code: 200,
                    message: "success",
                    data: data,
                })

            } else {
                res.send({
                    code: 500,
                    message: "fail"
                })
            }

        } else {
            res.send({
                code: 501,
                message: "dbfail"
            })
        }
    })

});
// 更新
router.post('/updateTake', function (req, res) {
    var sql = `UPDATE takeList SET takeContent=?,takeDesc = ?,takeImg=?,takeTags=?,takeTitle=?,takeValidDate=? WHERE takeId = ?`
    db(sql, [req.body.infoContent, req.body.infoDesc, req.body.infoImg, req.body.infoTags, req.body.infoTitle, req.body.infoValidDate, req.body.infoId], function (err, data) {
        //    db(sql,null,function(err,data){

        if (!err) {
            //    console.log(123);
            res.send({
                code: 200,
                message: "success",
                data: data,
            })
        } else {
            res.send({
                code: 501,
                message: "dbfail"
            })
        }
    })

});
// 图片批量更新  delBarterImg
router.post('/delTakeImg', function (req, res) {
    var sql = `DELETE FROM takeimg WHERE takeId = ?`
    db(sql, [req.body.infoId], function (err, data) {
        if (!err) {
            res.send({
                code: 200,
                message: "success",
                data: data,
            })
        } else {
            res.send({
                code: 501,
                message: "dbfail",
            })
        }
    });

});
router.post('/updateTakeImgs', function (req, res) {
    let arr = req.body.infoImgs; // 前端提交过来的数据
    console.log(arr, 11);
    let sql = `INSERT INTO takeimg (takeId,takeImg) VALUES(?,?)`    // 插入语句
    for (let i = 0; i < arr.length; i++) {
        db(sql, [req.body.infoId, arr[i]], function (err, data) {
            if (err) {
                console.log(err)
                if (i == arr.length - 1) {
                    res.send({
                        code: 500,
                        msg: "添加失败!",
                    });
                }
            } else {
                console.log(data);
                if (i == arr.length - 1) {
                    res.send({
                        msg: "添加成功!",
                        code: 200
                    });
                }
            }
        });
    }
});
// 获取留言内容
router.get('/message', function (req, res) {
    //   res.send('respond with a resource');
    console.log(req.query.messageId);
    var sql = `SELECT * FROM message where messageId = ?`
    db(sql, [req.query.infoId], function (err, data) {
        if (!err) {
            //    console.log(123);
            if (data.length > 0) {
                res.send({
                    code: 200,
                    message: "success",
                    data: data,
                })

            } else {
                res.send({
                    code: 500,
                    message: "fail"
                })
            }

        } else {
            res.send({
                code: 501,
                message: "dbfail"
            })
        }
    })

});
// 留言更新
router.post('/updateMessage', function (req, res) {
    var sql = `UPDATE messagelist SET messageDesc = ?,messageImg=?,messageTitle=? WHERE messageId = ?`
    db(sql, [req.body.infoDesc, req.body.infoImg, req.body.infoTitle, req.body.infoId], function (err, data) {
        //    db(sql,null,function(err,data){

        if (!err) {
            //    console.log(123);
            res.send({
                code: 200,
                message: "success",
                data: data,
            })
        } else {
            res.send({
                code: 501,
                message: "dbfail"
            })
        }
    })

});
// 分享更新
router.post('/updateShare', function (req, res) {
    var sql = `UPDATE sharelist SET shareDesc = ?,shareImg=?,shareTitle=? WHERE shareId = ?`
    db(sql, [req.body.infoDesc, req.body.infoImg, req.body.infoTitle, req.body.infoId], function (err, data) {
        //    db(sql,null,function(err,data){

        if (!err) {
            //    console.log(123);
            res.send({
                code: 200,
                message: "success",
                data: data,
            })
        } else {
            res.send({
                code: 501,
                message: "dbfail"
            })
        }
    })

});

// delBarter  删除寻物与出物
router.get('/delBarter', function (req, res) {
    var sql = `DELETE FROM barterlist WHERE barterId = ?;DELETE FROM barterimg WHERE barterId = ?;
            DELETE FROM bartermessage WHERE barterId = ?;DELETE FROM barterjubao WHERE barterId = ?`
    db(sql, [req.query.infoId, req.query.infoId, req.query.infoId, req.query.infoId], function (err, data) {
        if (!err) {
            res.send({
                code: 200,
                message: "success",
                data: data,
            })
        } else {
            res.send({
                code: 501,
                message: "dbfail"
            })
        }
    })
});
//  删除代取
router.get('/delTake', function (req, res) {
    var sql = `DELETE FROM takelist WHERE  takeId = ?;DELETE FROM  takeimg WHERE  takeId = ?;
            DELETE FROM  takemessage WHERE  takeId = ?;DELETE FROM  takejubao WHERE  takeId = ?`
    db(sql, [req.query.infoId, req.query.infoId, req.query.infoId, req.query.infoId], function (err, data) {
        if (!err) {
            res.send({
                code: 200,
                message: "success",
                data: data,
            })
        } else {
            res.send({
                code: 501,
                message: "dbfail"
            })
        }
    })
});
//  删除分享
router.get('/delShare', function (req, res) {
    var sql = `DELETE FROM sharelist WHERE  shareId = ?;`
    db(sql, [req.query.infoId], function (err, data) {
        if (!err) {
            res.send({
                code: 200,
                message: "success",
                data: data,
            })
        } else {
            res.send({
                code: 501,
                message: "dbfail"
            })
        }
    })
});
//  删除留言互助
router.get('/delMessage', function (req, res) {
    var sql = `DELETE FROM messagelist WHERE  messageId = ?;
            DELETE FROM  message WHERE  messageId = ?;DELETE FROM  messagejubao WHERE  messageId = ?`
    db(sql, [req.query.infoId, req.query.infoId, req.query.infoId], function (err, data) {
        if (!err) {
            res.send({
                code: 200,
                message: "success",
                data: data,
            })
        } else {
            res.send({
                code: 501,
                message: "dbfail"
            })
        }
    })
});
/* GET  */
router.get('/handleAll', function (req, res) {
    //   res.send('respond with a resource');
    var sql = `select barterId,barterType,barterAudit,barterTitle,barterImg,barterDesc from barterlist where leaveMessageAudit = 1 
    AND barterId = ANY(SELECT barterId FROM bartermessage where leaveUser = ?)  OR InfoPublisher = ?
    union ALL select takeId,takeType,takeAudit,takeTitle,takeImg,takeDesc from takelist where leaveMessageAudit = 1 
    AND takeId = ANY(SELECT takeId FROM takemessage where leaveUser = ?) OR InfoPublisher = ?`
    // ORDER BY barterId
    db(sql, [req.query.leaveUser, req.query.leaveUser, req.query.leaveUser, req.query.leaveUser], function (err, data) {
        if (!err) {
            //    console.log(123);
            if (data.length > 0) {
                res.send({
                    code: 200,
                    message: "success",
                    data: data,
                })

            } else {
                res.send({
                    code: 500,
                    message: "fail"
                })
            }

        } else {
            res.send({
                code: 501,
                message: "dbfail"
            })
        }
    })

});
// 寻物、出物
router.get('/handleBarter', function (req, res) {
    //   res.send('respond with a resource');
    var sql = `SELECT * FROM barterList where barterId = ?`
    db(sql, [req.query.infoId], function (err, data) {
        if (!err) {
            if (data.length > 0) {
                res.send({
                    code: 200,
                    message: "success",
                    data: data,
                })

            } else {
                res.send({
                    code: 500,
                    message: "fail"
                })
            }

        } else {
            res.send({
                code: 501,
                message: "dbfail"
            })
        }
    })

});
//   寻物留言内容获取
router.get('/barterMessages', function (req, res) {
    //   res.send('respond with a resource');
    var sql = `SELECT leaveMessage FROM bartermessage where barterId = ?`
    db(sql, [req.query.barterId], function (err, data) {
        if (!err) {
            if (data.length > 0) {
                res.send({
                    code: 200,
                    message: "success",
                    data: data,
                })

            } else {
                res.send({
                    code: 500,
                    message: "fail"
                })
            }

        } else {
            res.send({
                code: 501,
                message: "dbfail"
            })
        }
    })

});
//  寻物信息处理
router.get('/updateAudit', function (req, res) {
    //   res.send('respond with a resource');
    var sql = `UPDATE barterlist SET authMessage = ?,barterAudit = 0 WHERE barterId = ?`
    db(sql, [req.query.authMessage, req.query.barterId], function (err, data) {
        if (!err) {
            if (data.length > 0) {
                res.send({
                    code: 200,
                    message: "success",
                    data: data,
                })

            } else {
                res.send({
                    code: 500,
                    message: "fail"
                })
            }

        } else {
            res.send({
                code: 501,
                message: "dbfail"
            })
        }
    })

});
// 代取
router.get('/handleTake', function (req, res) {
    //   res.send('respond with a resource');
    var sql = `SELECT * FROM takeList where takeId = ?`
    db(sql, [req.query.infoId], function (err, data) {
        if (!err) {
            if (data.length > 0) {
                res.send({
                    code: 200,
                    message: "success",
                    data: data,
                })

            } else {
                res.send({
                    code: 500,
                    message: "fail"
                })
            }

        } else {
            res.send({
                code: 501,
                message: "dbfail"
            })
        }
    })

});
//   代取留言内容获取
router.get('/takeMessages', function (req, res) {
    //   res.send('respond with a resource');
    var sql = `SELECT leaveMessage FROM takemessage where takeId = ?`
    db(sql, [req.query.barterId], function (err, data) {
        if (!err) {
            if (data.length > 0) {
                res.send({
                    code: 200,
                    message: "success",
                    data: data,
                })

            } else {
                res.send({
                    code: 500,
                    message: "fail"
                })
            }

        } else {
            res.send({
                code: 501,
                message: "dbfail"
            })
        }
    })

});
//  寻物信息处理
router.get('/updateTakeAudit', function (req, res) {
    //   res.send('respond with a resource');
    var sql = `UPDATE takelist SET authMessage = ?,takeAudit = 0  WHERE takeId = ?`
    db(sql, [req.query.authMessage, req.query.barterId], function (err, data) {
        if (!err) {
            if (data.length > 0) {
                res.send({
                    code: 200,
                    message: "success",
                    data: data,
                })

            } else {
                res.send({
                    code: 500,
                    message: "fail"
                })
            }

        } else {
            res.send({
                code: 501,
                message: "dbfail"
            })
        }
    })

});











// 寻物举报
router.get('/jubaoBarter', function (req, res) {
    var sql = `INSERT into barterjubao (jubao,barterId,userId) VALUES (?,?,?);UPDATE barterlist SET barterAudit=2 WHERE barterId = ?`
    db(sql, [req.query.jubao, req.query.barterId, req.query.userId, req.query.barterId], function (err, data) {
        if (!err) {
            //    console.log(123);
            res.send({
                code: 200,
                message: "success",
                data: data,
            })
        } else {
            res.send({
                code: 501,
                message: "dbfail"
            })
        }
    });

});
// 寻物添加
router.post('/addBarter', function (req, res) {
    console.log(req.body);
    var sql = `INSERT into barterlist (InfoPublisher,barterAudit,
            barterContent,
            barterDesc,
            barterImg,
            barterTagType,
            barterTags,
            barterTitle,
            barterType,
            barterValidDate,
            leaveMessageAudit) VALUES (?)
            `
    //   select @phoen:=?
    // select @phoen:=barterId from barterlist WHERE InfoPublisher=? and barterAudit=? and 
    // barterContent=? and 
    // barterDesc=? and 
    // barterImg=? and 
    // barterTagType=? and 
    // barterTags=? and 
    // barterTitle=? and 
    // barterType=? and 
    // barterValidDate=? and 
    // leaveMessageAudit=?;
    // INSERT INTO barterimg (barterId) VALUES( @phoen);  

    let params = [req.body.InfoPublisher, req.body.barterAudit,
    req.body.barterContent,
    req.body.barterDesc,
    req.body.barterImg,
    req.body.barterTagType,
    req.body.barterTags,
    req.body.barterTitle,
    req.body.barterType,
    req.body.barterValidDate,
    req.body.leaveMessageAudit]

    db(sql, [params], function (err, data) {
        if (!err) {
            //    console.log(123);
            res.send({
                code: 200,
                message: "success",
                data: data,
            })
        } else {
            res.send({
                code: 501,
                message: "dbfail"
            })
        }
    });

});

// 寻物搜索
router.get('/searchTitle', function (req, res) {
    console.log(req.query.searchTitle);
    var sql = `select * from barterlist where barterTitle  REGEXP ?`
    db(sql, [req.query.searchTitle], function (err, data) {
        if (!err) {
            res.send({
                code: 200,
                message: "success",
                data: data,
            })
        } else {
            res.send({
                code: 501,
                message: "dbfail",
            })
        }
    });

});
module.exports = router;